<?php 
require_once "Model.php";
require_once "../Common/connDB.php";
	class UserModel extends Model{
		
		public function __construct(){
			parent::__construct();

		}

		public function getUserList(){
			
			$sql="select * from users";
			$data=$this->conn->query($sql);
			return $data;
		}

		//将用户注册信息写入数据库
		public function addUserInfo(){
			$pic=$this->uploadfile("pic",'../images');
			$uname=trim($_POST['username']);
			$password=$_POST['password'];
			$gender=$_POST['gender'];
			$birthdate=$_POST['birthdate'];
			$hobby=implode($_POST['hobby'], ',');
			$degree=$_POST['degree'];
			$intro=$_POST['intro'];
			
			$sql="insert into users values (null,'$uname',md5('$password'),'$gender','$birthdate','$hobby','$degree','$intro','$pic')";
			
			echo "$sql";
			$result=$this->conn->query($sql);
			return $result;
			}
			
			public function getUserbyID(){
				$uid=$_GET['uid'];
				$sql="select * from users where uid=$uid";
				$data=$this->conn->query($sql);
				return $data;
			}
			public function updateUserInfo(){
				
				$pic=$this->uploadfile("pic",'../images');
				$uid=$_POST['uid'];
				$uname=trim($_POST['uname']);
				$password=$_POST['password'];
				$gender=$_POST['gender'];
				$birthdate=$_POST['birthdate'];
				$hobby=implode($_POST['hobby'], ',');
				$degree=$_POST['degree'];
				$intro=$_POST['intro'];

				if($pic==null){
					$sql = "update users set uname='{$uname}',password=md5('{$password}'), birthdate='{$birthdate}', gender='{$gender}',hobby='{$hobby}',degree='{$degree}',intro='{$intro}', where uid=$uid";
				}else{
					$sql = "update users set uname='{$uname}',password=md5('{$password}'), birthdate='{$birthdate}', gender='{$gender}',hobby='{$hobby}',degree='{$degree}',intro='{$intro}',pic='{$pic}' where uid=$uid";
				}

				
				$result=$this->conn->query($sql);
				return $result;
			}
			public function del(){
				$uid=$_GET['uid'];
				$sql="delete from users where uid={$uid}";
				$result=$this->conn->query($sql);
				return $result;
			}
	}

 ?>